//
//  RootViewController.m
//  XRmenu
//
//  Created by shengchao yang on 11-11-23.
//  Copyright 2011年 foxhis. All rights reserved.
//

#import "XRRootViewController.h"

#import "CommendView.h"
#import "MainView.h"
#import "OemView.h"
#import "WelcomeView.h"

#import "MaskView.h"
#import "XRMaskView.h"
#import "MyOrderView.h"
#import "XRTextInputView.h"
#import "DishOrder.h"
#import "PhotoPopoverView.h"
#import "SendOrderView.h"

#define kTagOfStepView 5000
#define kTagOfStepViewPreRemove 5100
#define kTagOfAlter1 5880
#define kTagOfAlter2 5890
#define kTagOfAlter3 5910	// Akins
#define kTagOfAlter4 5920	

@implementation XRRootViewController
@synthesize currStep;
#pragma mark 应用逻辑
-(void) ViewAnimationStoped{
	while ([self.view viewWithTag:kTagOfStepViewPreRemove]) {
		[[self.view viewWithTag:kTagOfStepViewPreRemove] removeFromSuperview];
	}
    APPSESSION.isAnimating=NO;//标志动画结束
}

-(void) changeToStepView:(ViewStep)vs
{
	if ([self.view viewWithTag:kTagOfStepView]) {
        [[self.view viewWithTag:kTagOfStepView] setTag:kTagOfStepViewPreRemove];
        if (APPSESSION.isViewRemoveFromLef) {
            [[self.view viewWithTag:kTagOfStepViewPreRemove] slideOutTo:kFTAnimationLeft duration:0.6f delegate:self startSelector:nil stopSelector:@selector(ViewAnimationStoped)];
        }
        else{
            [[self.view viewWithTag:kTagOfStepViewPreRemove] slideOutTo:kFTAnimationRight duration:0.6f delegate:self startSelector:nil stopSelector:@selector(ViewAnimationStoped)];
        }
	}
    else{
        APPSESSION.isAnimating=NO;//标志动画结束
    }
	if (vs == ViewOfOEM) {
		//如果有oem图片存在
		XRmenuError *error = nil;
		NSString *oem = [XRmenuFileManage getOEMImageFile:&error];
        if (error == nil && [[NSUserDefaults standardUserDefaults] boolForKey:@"oem_switch"]) {
			OemView *oemV = [[OemView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768) OEM:oem];
			oemV.tag = kTagOfStepView;
			oemV.hidden = YES;
			[self.view addSubview:oemV];
			[oemV release];		
			
			[[self.view viewWithTag:kTagOfStepView] fallIn:0.5 delegate:nil];
		}else {
			NSDictionary *infos1 = [NSDictionary dictionaryWithObjectsAndKeys:num(ViewOfWelcome), @"Step", nil];
            postNWithInfos(kMessage_RootView_ChangeStep, self, infos1);
		}
	}
    else if (vs == ViewOfWelcome) {
		WelcomeView *welcomeView = [[WelcomeView alloc] initWithStyle:APPSESSION.uistyle ViewName:@"Welcome View"];
        welcomeView.tag = kTagOfStepView;
        if ([self.view viewWithTag:kTagOfStepViewPreRemove]) {
            [self.view insertSubview:welcomeView belowSubview:[self.view viewWithTag:kTagOfStepViewPreRemove]];
        }else{
            [self.view addSubview:welcomeView];
        }
        [welcomeView release];		
	}
    else if (vs == ViewOfCommend) {
        CommendView *cmdView = [[CommendView alloc] initWithStyle:APPSESSION.uistyle  ViewName:@"Commend View"];
        cmdView.tag = kTagOfStepView;
        if ([self.view viewWithTag:kTagOfStepViewPreRemove]) {
            [self.view insertSubview:cmdView belowSubview:[self.view viewWithTag:kTagOfStepViewPreRemove]];
        }else{
            [self.view addSubview:cmdView];
        }
        [cmdView release];
	}
    else if (vs == ViewOfProduct) {
        MainView *mainView = [[MainView alloc] initWithStyle:APPSESSION.uistyle ViewName:@"Main View"];
        mainView.tag = kTagOfStepView;
        if ([self.view viewWithTag:kTagOfStepViewPreRemove]) {
            [self.view insertSubview:mainView belowSubview:[self.view viewWithTag:kTagOfStepViewPreRemove]];
        }else{
            [self.view addSubview:mainView];
        }
        [mainView release];
	}
}

-(void) needChangeCurrentView:(NSNotification *)note{
    APPSESSION.isAnimating=YES;//标志动画开始
	NSDictionary *infos = [note userInfo];
	if ([infos objectForKey:@"Step"]) {
		ViewStep vs = (ViewStep)[[infos objectForKey:@"Step"] intValue];
		[self changeToStepView:vs];
	}
}
#pragma mark - 系统性框架代码
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    
    addNObserver(@selector(needChangeCurrentView:), kMessage_RootView_ChangeStep);
    //弹出窗口一 大图显示详细信息
    addNObserver(@selector(PhotoPopoverViewDisplay), kMessage_RootView_PhotoPopoverViewDisplay);
    addNObserver(@selector(PhotoPopoverViewDisappear), kMessage_RootView_PhotoPopoverViewDisappear);
    //弹出窗口二 我的点单
    addNObserver(@selector(MyOrderViewDisplay), kMessage_RootView_MyOrderViewDisplay);
    addNObserver(@selector(MyOrderViewDisappear), kMessage_RootView_MyOrderViewDisappear);
	//弹出窗口三 增加菜品备注输入窗口 by Akins 2011-12-23
	addNObserver(@selector(PluNotesViewDisplay:), kMessage_RootView_PluNotesViewDisplay);
	addNObserver(@selector(PluNotesViewDisappear), kMessage_RootView_PluNotesViewDisappear);
    //弹出窗口四 用户名密码输入窗口
    addNObserver(@selector(SendOrderViewDisplay), kMessage_RootView_SendOrderViewDisplay);
	addNObserver(@selector(SendOrderViewDisappear), kMessage_RootView_SendOrderViewDisappear);
    [self changeToStepView:ViewOfOEM];
}

#pragma mark - 弹出窗口一显示详细菜品信息
///////////////弹出窗口一//////////////////////
//弹出窗口显示详细菜品信息(只通过事件触发被子view调用)
-(void) PhotoPopoverViewDisplay{
    MaskView *mask=[[MaskView alloc] initWithStyle:APPSESSION.uistyle DishSubViewName:@"Main View/Mask View"];
    mask.tag=kTagOfAlter1;
    [self.view addSubview:mask];
    
    PhotoPopoverView *ppv=[[PhotoPopoverView alloc] initWithStyle:APPSESSION.uistyle DishSubViewName:@"Main View/Photo Popover View" WithPluPlu:APPSESSION.currentPluPlu];
    ppv.tag=kTagOfAlter1+1;
    [XRMaskView createWithWrapperView:mask contentView:ppv];
    [ppv release];
    [mask release];
}

//退出详细菜品信息界面(只通过事件触发被子view调用)
-(void) PhotoPopoverViewDisappear{
    if ([self.view viewWithTag:kTagOfAlter1]) {
        [[self.view viewWithTag:kTagOfAlter1] fadeOut:0.2f delegate:self startSelector:nil stopSelector:@selector(MaskViewMoveOut)];
    }
}
-(void) MaskViewMoveOut{
    [[self.view viewWithTag:kTagOfAlter1] removeFromSuperview];
}

#pragma mark - 弹出窗口二显示我的点单界面
///////////////弹出窗口二//////////////////
//弹出窗口显示我的点单界面
-(void) MyOrderViewDisplay{
    MaskView *mask=[[MaskView alloc] initWithStyle:APPSESSION.uistyle DishSubViewName:@"Main View/Mask View"];
    mask.tag=kTagOfAlter2;
    [self.view addSubview:mask];
    MyOrderView *ppv=[[MyOrderView alloc] initWithStyle:APPSESSION.uistyle DishSubViewName:@"Main View/MyOrder View"];
    ppv.tag=kTagOfAlter2+1;
    [XRMaskView createWithWrapperView:mask contentView:ppv];
    [ppv release];
    [mask release];
}
//退出我的点单界面
-(void) MyOrderViewDisappear{
    if ([self.view viewWithTag:kTagOfAlter2]) {
        [[self.view viewWithTag:kTagOfAlter2] fadeOut:0.2f delegate:self startSelector:nil stopSelector:@selector(MaskViewMoveOut1)];
    }
}
-(void) MaskViewMoveOut1{
    [[self.view viewWithTag:kTagOfAlter2] removeFromSuperview];
    postN(kMessage_DishplayModeView_UpdateData);//通知修改显示模式界面里的选中状态
}

#pragma mark - 弹出窗口三输入菜品备注信息
////////////弹出窗口三////////////////
//弹出窗口输入菜品备注信息
-(void) PluNotesViewDisplay:(NSNotification *)sender {
	NSDictionary *odata = [sender userInfo];
	DishOrder *celldata = (DishOrder*)[odata objectForKey:@"OrderData"];
	MaskView *mask=[[MaskView alloc] initWithStyle:APPSESSION.uistyle DishSubViewName:@"Main View/Mask View"];
    mask.tag=kTagOfAlter3;
    [self.view addSubview:mask];
    
    XRTextInputView *ppv=[[XRTextInputView alloc] initWithOrderData:celldata];
    ppv.tag=kTagOfAlter3+1;
    [XRMaskView createWithWrapperView:mask contentView:ppv];
    [ppv release];
    [mask release];
}
//退出输入菜品备注窗口
-(void) PluNotesViewDisappear{
    if ([self.view viewWithTag:kTagOfAlter3]) {
//        [[self.view viewWithTag:kTagOfAlter3] slideOutTo:kFTAnimationBottom duration:0.1 delegate:self startSelector:nil stopSelector:@selector(NotesTextViewMoved)];
        [[self.view viewWithTag:kTagOfAlter3] fadeOut:0.2f delegate:self startSelector:nil stopSelector:@selector(NotesTextViewMoved)];
    }
}
-(void)NotesTextViewMoved{
	DebugLog(@"备注窗口有的话就移除");
	while ([self.view viewWithTag:kTagOfAlter3]) {
		[[self.view viewWithTag:kTagOfAlter3] removeFromSuperview];
	}
}

#pragma mark - 弹出窗口四显示送单界面
///////////////弹出窗口四//////////////////
//弹出窗口显示送单界面
-(void) SendOrderViewDisplay{
    MaskView *mask=[[MaskView alloc] initWithStyle:APPSESSION.uistyle DishSubViewName:@"Main View/Mask View"];
    mask.tag=kTagOfAlter4;
    [self.view addSubview:mask];
    
    SendOrderView *ppv=[[SendOrderView alloc] init];
    ppv.tag=kTagOfAlter4+1;
    [XRMaskView createWithWrapperView:mask contentView:ppv];
    [ppv release];
    [mask release];
}
//退出我的点单界面
-(void) SendOrderViewDisappear{
    if ([self.view viewWithTag:kTagOfAlter4]) {
        [[self.view viewWithTag:kTagOfAlter4] fadeOut:0.2f delegate:self startSelector:nil stopSelector:@selector(SendOrderViewMoved)];
    }
}
-(void) SendOrderViewMoved{
    [[self.view viewWithTag:kTagOfAlter4] removeFromSuperview];
}

#pragma mark - 基类委托方法
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft) ;
}
- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}
- (void)dealloc {
    removeNObserver(kMessage_RootView_ChangeStep);
    
    removeNObserver(kMessage_RootView_PhotoPopoverViewDisplay);
    removeNObserver(kMessage_RootView_PhotoPopoverViewDisappear);
    
    removeNObserver(kMessage_RootView_MyOrderViewDisplay);
    removeNObserver(kMessage_RootView_MyOrderViewDisappear);
    // 增加相应的消息注销 by Akins at 2011-12-23
	removeNObserver(kMessage_RootView_PluNotesViewDisplay);
	removeNObserver(kMessage_RootView_PluNotesViewDisappear);
    
    removeNObserver(kMessage_RootView_SendOrderViewDisplay);
    removeNObserver(kMessage_RootView_SendOrderViewDisappear);
    [super dealloc];
}


@end